﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Web.BaseCode;

namespace Web
{
    public partial class Content : System.Web.UI.Page
    {
        public string articleContent = string.Empty;
        public string addtime = string.Empty;
        public string publishTime = string.Empty;
        public string articleUrl = string.Empty;
        public string articleName = string.Empty;
        public string articleEName = string.Empty;
        public string articleClass = string.Empty;
        public string articleTags = string.Empty;
        public string keywords = string.Empty;
        public string description = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ename"] != null)
                {
                    string ename = Request.QueryString["ename"].ToString();
                    this.BindData(ename);
                }
                else
                {
                    //跳转404
                }
            }
        }

        /// <summary>
        /// 显示文章列表
        /// </summary>
        void BindData(string ename)
        {
            if (!this.ArticleInfo(ename))
            {
                //跳转404
            }
        }

        /// <summary>
        /// 显示文章列表
        /// </summary>
        private bool ArticleInfo(string ename)
        {
            BLL.BLL_Article bll = new BLL.BLL_Article();
            Model.Model_Article article = bll.GetModel(ename);

            if (article != null)
            {
                //获取文章分类
                BLL.BLL_Tags bllTag = new BLL.BLL_Tags();
                List<Model.Model_Tags> ModelClass = bllTag.GetModelList(string.Format(" tagtype={0} and id in (select tagsid from tags_art where articleid = {1})", CommValue.ArticleClass, article.id));
                string tmpClass = string.Empty;
                if (ModelClass != null)
                {
                    for (int i = 0; i < ModelClass.Count; i++)
                    {
                        tmpClass = string.Format("{0},{1}", tmpClass, ModelClass[i].name);
                        this.articleClass = string.Format("<a href=\"{0}\" title=\"{1}\" rel=\"tag\">{2}</a>",
                            CommMethod.GetUrl(BlogPageType.Category, ModelClass[i].ename),
                            ModelClass[i].name, ModelClass[i].name);
                    }
                }

                //获取文章标签
                List<Model.Model_Tags> ModelTags = bllTag.GetModelList(string.Format(" tagtype={0} and id in (select tagsid from tags_art where articleid = {1})", CommValue.ArticleTag, article.id));

                string tmpTags = string.Empty;
                if (ModelTags != null)
                {
                    for (int i = 0; i < ModelTags.Count; i++)
                    {
                        tmpTags = string.Format("{0},{1}", tmpTags, ModelTags[i].name);
                        if (string.IsNullOrEmpty(this.articleTags))
                        {
                            this.articleTags = string.Format("<a href=\"{0}\" title=\"{1}\" rel=\"tag\">{2}</a>",
                                CommMethod.GetUrl(BlogPageType.Tag, ModelTags[i].ename),
                                ModelTags[i].name, ModelTags[i].name);
                        }
                        else
                        {
                            this.articleTags = string.Format("{3}、<a href=\"{0}\" title=\"{1}\" rel=\"tag\">{2}</a>",
                                CommMethod.GetUrl(BlogPageType.Tag, ModelTags[i].ename),
                                ModelTags[i].name, ModelTags[i].name, this.articleTags);
                        }
                    }
                }

                this.description = Maticsoft.Common.HtmlHelper.SubstringWithNoHTML(article.contents, BaseCode.CommValue.ArticleLength);
                this.keywords = string.Format("{0}{1}{2}", article.keywords, tmpTags, tmpClass);
                this.articleContent = article.contents;
                this.articleEName = article.ename;
                this.articleName = article.name;
                this.articleUrl = CommMethod.GetUrl(BlogPageType.Article, ename);
                this.publishTime = article.publishTime.ToString("yyyy-MM-dd");
                this.addtime = article.addtime.ToString("yyyy-MM-dd HH:mm:ss");

                return true;
            }
            else
            {
                return false;
            }
        }
    }
}